package com.zjw.zy.componse.demo.fragment

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.viewinterop.AndroidView
import com.zjw.zy.componse.demo.base.BaseComposeFragment
import com.zjw.zy.componse.demo.ui.theme.MyApplicationTheme
import com.zjw.zy.componse.demo.view.GobangView

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2023/2/20 10:16
 * @description：五子棋
 */
class GobangGameFragment : BaseComposeFragment() {
    override fun onContentView(rootView: ComposeView) {
        rootView.setContent {
            MyApplicationTheme {
                Box {

                    val result = remember {
                        mutableStateOf(false)
                    }

                    val gameView = remember {
                        GobangView(requireContext()).apply {
                            this.bindResultListener(object : GobangView.Listener {
                                override fun onResult(event: Int) {
                                    result.value = true
                                }
                            })
                        }
                    }

                    AndroidView(
                        factory = {
                            gameView
                        }, modifier = Modifier.fillMaxSize()
                    )

                    if (result.value) {
                        Button(onClick = {
                            result.value = false
                            gameView.initGame()
                        }, modifier = Modifier.align(Alignment.BottomCenter)) {
                            Text(text = "重新开始")
                        }
                    }
                }
            }
        }
    }

    override fun onSubscribeValue() {

    }
}